package com.creditease.tmsp.modules.sys.web;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import com.creditease.tmsp.common.config.Global;
import com.creditease.tmsp.common.persistence.IdGenerator;
import com.creditease.tmsp.common.web.BaseController;
import com.creditease.tmsp.modules.sys.entity.SysDict;
import com.creditease.tmsp.modules.sys.service.SysDictService;
import com.github.miemiedev.mybatis.paginator.domain.PageList;

/**
 * 
 * @Description：数据字典Controller
 * @author：
 * @CreateDate：
 * @Company:
 * @version 
 */

@Controller
@RequestMapping(value = "${adminPath}/sys/sysDict")
public class SysDictController extends BaseController {

	@Autowired
	private SysDictService sysDictService;
	
	@ModelAttribute
	public SysDict get(@RequestParam(required = false) String id) {
		if (StringUtils.isNotBlank(id)) {
			return sysDictService.getById(id);
		} else {
			return new SysDict();
		}
	}
	
	/**
	 * 列表
	 */
	@RequestMapping(value = {"list", ""})
	public String list(SysDict sysDict, HttpServletRequest request,
	           HttpServletResponse response, Model model){
		
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("creatorId", getPrincipal().getId());
		map.put("method", "list");
		map.putAll(convertBeanToMap(sysDict));
		
		List<SysDict> sysDictList = sysDictService.query(requestPageBounds(request), map);
		model.addAttribute("sysDictList", (PageList<SysDict>)sysDictList);
		return "modules/sys/sysDictList";
	}
	
	/***
	 * 表单
	 */
	@RequestMapping(value = "form")
	public String form(SysDict sysDict, Model model) {
		
		model.addAttribute("sysDict", sysDict);
		return "modules/sys/sysDictForm";
	}
	
	

	/**
	 * 增加/修改
	 */
	
	@RequestMapping(value = "save")
	public String save(SysDict sysDict, HttpServletRequest request,
			Model model, RedirectAttributes redirectAttributes) {
		
		if (!beanValidator(model, sysDict)) {
			return form(sysDict, model);
		}
		if (StringUtils.isNotBlank(sysDict.getId())) {
			sysDictService.update(sysDict); 
		}else{
			sysDict.setId(IdGenerator.getId()); //规则UUID
			sysDict.setDelFlag("0");            //删除标记（0：默认未删除）
			sysDictService.save(sysDict);
		}
		
		addMessage(redirectAttributes, "保存字典：'" + sysDict.getLabel() + "'成功");
		return "redirect:" + Global.getAdminPath() + "/sys/sysDict";
		
	}
	
	
	/**
	 * 删除(逻辑)
	 */
	
	@RequestMapping(value = "delete")
	public String delete(String id, RedirectAttributes redirectAttributes) {
		sysDictService.delete(id);
		addMessage(redirectAttributes, "删除成功");
		return "redirect:"+Global.getAdminPath()+"/sys/sysDict";
	}
	
	
}
